package com.example.demo.com.example.singleton;

import com.sun.imageio.plugins.common.LZWCompressor;

public class LazyInnerClassSingleton {

    //私有化构造器
    private LazyInnerClassSingleton(){}
    public int a = 100;

    static {
        System.out.println("静态块执行");
    }
    //提供调用方法
    public static LazyInnerClassSingleton getInstance(){
        System.out.println("获取实例");
        return InnerClassSingleton.lazyInnerClassSingleton;
    }

    static class InnerClassSingleton{
        final static LazyInnerClassSingleton lazyInnerClassSingleton = new LazyInnerClassSingleton();
        static {
            System.out.println("内部类静态块执行");
        }
    }

    //防止序列化反序列化破坏单例
    private Object readResolve(){
        return InnerClassSingleton.lazyInnerClassSingleton;
    }

    public static void main(String[] args) {
        LazyInnerClassSingleton  singleton = LazyInnerClassSingleton.getInstance();
        System.out.println(singleton.a);


    }
}
